﻿@using System.Activities.Expressions
@using PerpetuumSoft.Knockout
@using TimeProject.WebSite.BootstrapSupport
@using TimeProject.WebSite.Views;

@model TimeProject.WebSite.ViewModels.MyOrganizationsViewModel
@{
    var ko = Html.CreateKnockoutContext();
}

@using (ko.With(m => m.Messages))
{
    @Html.GetInfoBox(Model.Messages)
}

<div class="app-content-center">
    <form class="form-horizontal">
        <div class="page-header">
            <h1>MY ORGANIZATIONS <small>View and manage my organizations.</small></h1>
        </div>

        <div class="well">
            <table class="table table-condensed">
                <thead>
                    @{ var header = Model.Organizations.FirstOrDefault(); }
                    <tr>
                        <th style="width: 15%">@Html.DisplayNameFor(x => header.PublicId)</th>
                        <th style="width: 15%">@Html.DisplayNameFor(x => header.Code)</th>
                        <th style="width: 30%">@Html.DisplayNameFor(x => header.FullName)</th>
                        <th style="width: 20%">@Html.DisplayNameFor(x => header.MyRole)</th>
                        <th style="width: 20%"></th>
                    </tr>
                </thead>

                <tbody>
                    @using (var organization = ko.Foreach(m => m.Organizations))
                    {
                        @organization.Html.TrBegin(organization, "ShowOrganization").Css("activeTableRow", x => x.IsSelected).Css("inactiveTableRow", x => !x.IsSelected)
                        @organization.Html.GetTd(x => x.PublicId, organization)
                        @organization.Html.GetTd(x => x.Code, organization)
                        @organization.Html.GetTd(x => x.FullName, organization)
                        @organization.Html.GetTd(x => x.MyRole, organization)
                        <td>
                            @using (organization.If(x => x.IsDeleteAllowed))
                            {
                                @organization.Html.GetTdImageLink("Delete", "DeleteOrganization", "fa fa-times", new { index = "$index()" })
                            }
                        </td>
                        @organization.Html.TrEnd(organization)
                    }
                </tbody>
            </table>
            @ko.Html.GetImageLinkRight("Join", "JoinOrganization", "fa fa-sign-in").Enable(x => x.IsJoinOrganizationAllowed)
            @ko.Html.GetImageLinkRight("New", "CreateNewOrganization", "fa fa-plus-circle").Enable(x => x.IsCreateNewOrganizationAllowed)
            <p><div><br /></div></p>
        </div>

        <div>
            @using (var selected = ko.Foreach(x => x.Organizations))
            {
                using (selected.If(x => x.IsSelected))
                {
                    <p class="paragraph-break-well" />
                    <div class="well">
                        @Html.GetSubHeading("Organization details")

                        <div class="row-fluid">
                            <div class="span6">
                                <div class="control-group">
                                    <div class="control-label">
                                        @selected.Html.GetLabel(x => x.PublicId)
                                    </div>
                                    <div class="controls">
                                        @selected.Html.GetReadonlyTextBox(x => x.PublicId)
                                    </div>
                                </div>

                                <div class="control-group">
                                    <div class="control-label">
                                        @selected.Html.GetLabel(x => x.Code)
                                    </div>
                                    <div class="controls">
                                        @selected.Html.GetTextBox(x => x.Code).Enable(x => x.IsManageAllowed)
                                    </div>
                                </div>

                                <div class="control-group">
                                    <div class="control-label">
                                        @selected.Html.GetLabel(x => x.FullName)
                                    </div>
                                    <div class="controls">
                                        @selected.Html.GetTextBoxLong(x => x.FullName).Enable(x => x.IsManageAllowed)
                                    </div>
                                </div>

                            </div>
                            <div class="span6">
                                <div class="control-group">
                                    <div class="control-label">
                                        @selected.Html.GetLabel(x => x.Description)
                                    </div>
                                    <div class="controls">
                                        @selected.Html.GetTextArea(x => x.Description).Enable(x => x.IsManageAllowed)
                                    </div>
                                </div>

                            </div>
                        </div>



                        <table class="table table-striped table-hover table-condensed">
                            <caption>Members</caption>
                            <thead>
                                @{ var header2 = Model.Organizations.FirstOrDefault().Members.FirstOrDefault(); }
                                <tr>
                                    <th style="width: 15%">@Html.DisplayNameFor(x => header2.PublicId)</th>
                                    <th style="width: 30%">@Html.DisplayNameFor(x => header2.FullName)</th>
                                    <th style="width: 15%">@Html.DisplayNameFor(x => header2.Role)</th>
                                    <th style="width: 20%">@Html.DisplayNameFor(x => header2.Status)</th>
                                    <th style="width: 20%"></th>
                                </tr>
                            </thead>

                            <tbody>
                                @using (var member = selected.Foreach(m => m.Members))
                                {
                                    <tr>
                                        @member.Html.GetTd(x => x.PublicId, member)
                                        @member.Html.GetTd(x => x.FullName, member)
                                        @member.Html.GetTd(x => x.Role, member)
                                        @member.Html.GetTd(x => x.Status, member)
                                        <td></td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                }
            }
        </div>

    </form>
</div>

<div class="app-bottom-bar">
    <div class="app-bottom-bar-container">
        @Html.GetFixedBackLink("Index")
        @ko.Html.GetImageLink("Undo", "UndoMyOrganizationsChanges", "fa fa-undo").Enable(x => x.isDirty)
        @ko.Html.GetButtonRight("Save", "MyAccountSSSS", "fa fa-floppy-o").Enable(x => x.isDirty)

    </div>
</div>

@ko.Init(Model)




@*<div class="app-bottom-bar">
    <div class="container-fluid app-bottom-bar-container">
        <span class="app-bottom-bar-container-text">
            @Html.GetCopyright()
        </span>
    </div>
</div>*@

